import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from 'vue-router'
import { store } from '@/store'

import { UserLayout, BaseLayout, BlankLayout } from '@/layout'

const routes = [
  {
    path: '/user',
    name: 'user',
    component: UserLayout,
    children: [
      {
        path: 'login',
        name: 'login',
        component: () =>
          import(/* webpackChunkName: "user" */ '@/views/user/login.vue'),
      },
    ],
  },
  // 骑手专用配送页面
  {
    path: '/send',
    name: 'send',
    component: BlankLayout,
    redirect: '/send/index',
    children: [
      {
        path: 'index',
        name: 'send-order',
        component: () =>
          import(/* webpackChunkName: "send" */ '@/views/send/index.vue'),
      },
    ],
  },
  {
    path: '',
    name: 'home',
    component: BaseLayout,
    children: [
      {
        path: 'home',
        name: 'home',
        component: () =>
          import(/* webpackChunkName: "user" */ '@/views/home/index.vue'),
      },
      // 订单管理
      {
        path: 'order',
        name: 'order',
        component: BlankLayout,
        redirect: '/order/list',
        children: [
          {
            path: 'list',
            name: 'orderList',
            component: () =>
              import(/* webpackChunkName: "order" */ '@/views/order/index.vue'),
          },
          // 订单管理
          {
            path: 'detail',
            name: 'orderDetail',
            component: () =>
              import(
                /* webpackChunkName: "order" */ '@/views/order/detail.vue'
              ),
          },
        ],
      },
      // 字典管理
      {
        path: 'dict',
        name: 'dict',
        component: BlankLayout,
        redirect: '/dict/list',
        children: [
          {
            path: 'list',
            name: 'dictList',
            component: () =>
              import(/* webpackChunkName: "dict" */ '@/views/dict/index.vue'),
          },
        ],
      },
      // 分类管理
      {
        path: 'category',
        name: 'category',
        component: BlankLayout,
        redirect: '/category/list',
        children: [
          {
            path: 'list',
            name: 'categoryList',
            component: () =>
              import(
                /* webpackChunkName: "category" */ '@/views/category/index.vue'
              ),
          },
        ],
      },
      // 商品管理
      {
        path: 'product',
        name: 'product',
        component: BlankLayout,
        redirect: '/product/list',
        children: [
          {
            path: 'list',
            name: 'productList',
            component: () =>
              import(
                /* webpackChunkName: "product" */ '@/views/product/index.vue'
              ),
          },
        ],
      },
      // 广告管理
      {
        path: 'advertise',
        name: 'advertise',
        component: BlankLayout,
        redirect: '/advertise/list',
        children: [
          {
            path: 'list',
            name: 'advertiseList',
            component: () =>
              import(
                /* webpackChunkName: "advertise" */ '@/views/advertise/index.vue'
              ),
          },
        ],
      },
      // 骑手管理
      {
        path: 'rider',
        name: 'rider',
        component: BlankLayout,
        redirect: '/rider/list',
        children: [
          {
            path: 'list',
            name: 'riderList',
            component: () =>
              import(/* webpackChunkName: "rider" */ '@/views/rider/index.vue'),
          },
        ],
      },
      // 账号管理
      {
        path: 'account',
        name: 'account',
        component: BlankLayout,
        redirect: '/account/list',
        children: [
          {
            path: 'list',
            name: 'accountList',
            component: () =>
              import(
                /* webpackChunkName: "account" */ '@/views/account/index.vue'
              ),
          },
          {
            path: 'detail',
            name: 'accountDetail',
            component: () =>
              import(
                /* webpackChunkName: "account" */ '@/views/account/detail.vue'
              ),
          },
        ],
      },
    ],
  },
  {
    path: '/exception',
    name: 'exception',
    component: BlankLayout,
    children: [
      {
        path: '404',
        name: '404',
        component: () =>
          import(
            /* webpackChunkName: "exception" */ '@/views/exception/404.vue'
          ),
      },
      {
        path: '403',
        name: '403',
        component: () =>
          import(
            /* webpackChunkName: "exception" */ '@/views/exception/403.vue'
          ),
      },
      {
        path: '500',
        name: '500',
        component: () =>
          import(
            /* webpackChunkName: "exception" */ '@/views/exception/500.vue'
          ),
      },
    ],
  },
]

const router = createRouter({
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes,
})

const whiteList = ['/user/login']

router.beforeEach((to, from, next) => {
  if (!store.state.app.token && !whiteList.includes(to.path)) {
    return next('/user/login')
  } else {
    return next()
  }
})

export default router
